package com.kingcore.director.setting.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class TestTable extends JFrame implements ActionListener {
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
JTable jt;
  DefaultTableModel dtm;
  JLabel nameLabel, ageLabel, birthdayLabel;
  JTextField nameTF, ageTF, birthdayTF;
  JButton addButton;

  public TestTable() {
    Object[][] data = {
       {
        "Jackson", new Integer(4), "March 21"
      }, {
        "Zachary", new Integer(2), "May 12"
      }
    };
    String[] headers = {
      "Name", "Age", "Birthday"
    };

    // A DefaultTableModel object is created and initialized with some
    // data and headers.

    dtm = new DefaultTableModel(2, 3);
    dtm.setDataVector(data, headers);

    // A JTable object is created using the DefaultTableModel object
    // as its data model.

    jt = new JTable(dtm);
    jt.setGridColor(Color.red);
    jt.setRowSelectionAllowed(true);
    jt.setRowSelectionInterval(1, 1);

    // Some GUI components are added that are used to add rows to
    // the JTable.

    nameLabel = new JLabel("Name: ");
    nameLabel.setForeground(Color.black);
    ageLabel = new JLabel("   Age: ");
    ageLabel.setForeground(Color.black);
    birthdayLabel = new JLabel("   Birthday: ");
    birthdayLabel.setForeground(Color.black);

    nameTF = new JTextField(10);
    ageTF = new JTextField(3);
    birthdayTF = new JTextField(12);

    addButton = new JButton("add");
    addButton.addActionListener(this);

    JPanel p = new JPanel();
    p.add(nameLabel);
    p.add(nameTF);
    p.add(ageLabel);
    p.add(ageTF);
    p.add(birthdayLabel);
    p.add(birthdayTF);
    p.add(addButton);

    // The JTable is placed inside a JScrollPane before it is placed
    // within the JFrame

    JPanel pc = new JPanel();
    pc
      .add(new JScrollPane(jt, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                           JScrollPane
                             .HORIZONTAL_SCROLLBAR_AS_NEEDED));

    getContentPane().add(pc, BorderLayout.CENTER);
    getContentPane().add(p, BorderLayout.SOUTH);

    addWindowListener(new WindowClosing());
    setBounds(100, 100, 700, 500);
    setVisible(true);
  }

  // whenever the "add" JButton is pressed, the actionPerformed()
  // method is called and a new row is added to the JTable

  public void actionPerformed(ActionEvent ae) {
    String name = nameTF.getText();
    Integer age = new Integer(ageTF.getText());
    String birthday = birthdayTF.getText();

    Object[] newData = {
      name, age, birthday
    };
    dtm.addRow(newData);
  } 

  public static void main(String[] args) {
    TestTable tjt = new TestTable();
  } 
}

// The WinClosing class terminates the program when the window is closed

class WinClosing extends WindowAdapter {
  public void windowClosing(WindowEvent we) {
    System.exit(0);
  } 
}
